package ch.bfh.ti.pixelator.camera;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class CameraManager {
	private Context context;

	public CameraManager(Context context) {
		this.context = context;
	}

	public Camera OpenSelectedCamera() {
		Camera camera = null;

		// Get the camera preference
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		boolean useFrontCamera = pref.getBoolean("cameraPref", false);

		try {
			Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
			for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
				Camera.getCameraInfo(i, cameraInfo);
				if ((!useFrontCamera && cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
						|| (useFrontCamera && cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT))
					camera = Camera.open(i);
			}
		} catch (RuntimeException e) {
			Toast.makeText(context, "Could not open camera: " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		if (camera == null) {
			Toast.makeText(context, "Could not open camera!", Toast.LENGTH_LONG)
					.show();
		}

		return camera;
	}

	public Camera OpenSelectedCameraWithDefaultParameters() {
		Camera camera = OpenSelectedCamera();
		if (camera == null)
			return null;

		// Get the camera preference
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		int cameraResolution = Integer.parseInt(pref.getString(
				"cameraResolution", "240"));
		
		Size previewSize = null;
		List<Size> supportedPreviewSizes = camera.getParameters()
				.getSupportedPreviewSizes();

		// Take the preview size with the smallest width
		int width = Integer.MAX_VALUE;
		if (supportedPreviewSizes != null) {
			for (Size size : supportedPreviewSizes) {
				if (size.height == cameraResolution && size.width < width) {
					width = size.width;
					previewSize = size;
				}
			}
		}

		Camera.Parameters params = camera.getParameters();

		params.setPreviewSize(previewSize.width, previewSize.height);

		params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);

		List<String> focusModes = params.getSupportedFocusModes();
		if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
			params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

		params.setPreviewFormat(ImageFormat.NV21);
		camera.setDisplayOrientation(90);

		if (params.getMaxNumMeteringAreas() > 0) {
			List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();

			Rect center = new Rect(-100, -100, 100, 100);
			meteringAreas.add(new Camera.Area(center, 1000));
			params.setMeteringAreas(meteringAreas);
		}

		camera.setParameters(params);

		byte[] frame = new byte[previewSize.width * previewSize.height * 2];
		camera.addCallbackBuffer(frame);

		return camera;
	}
}
